#!/usr/bin/env bash

#需要配置如下参数
# export PROJ_PATH=项目源代码路径
# export APP_PATH=/apprun 项目运行路径

killEureka()
{
    pid=`ps -ef|grep we-eureka|grep java|awk '{print $2}'`
    echo "tomcat Id list :$pid"
    if [ "$pid" = "" ]
    then
      echo "no tomcat pid alive"
    else
      kill -9 $pid
    fi
}
killConfig()
{
    pid=`ps -ef|grep we-config|grep java|awk '{print $2}'`
    echo "tomcat Id list :$pid"
    if [ "$pid" = "" ]
    then
      echo "no tomcat pid alive"
    else
      kill -9 $pid
    fi
}

killUpms()
{
    pid=`ps -ef|grep we-upms-service|grep java|awk '{print $2}'`
    echo "tomcat Id list :$pid"
    if [ "$pid" = "" ]
    then
      echo "no tomcat pid alive"
    else
      kill -9 $pid
    fi
}

killAuth()
{
    pid=`ps -ef|grep we-auth|grep java|awk '{print $2}'`
    echo "tomcat Id list :$pid"
    if [ "$pid" = "" ]
    then
      echo "no tomcat pid alive"
    else
      kill -9 $pid
    fi
}

killGateway()
{
    pid=`ps -ef|grep we-gateway|grep java|awk '{print $2}'`
    echo "tomcat Id list :$pid"
    if [ "$pid" = "" ]
    then
      echo "no tomcat pid alive"
    else
      kill -9 $pid
    fi
}
##构建项目
cd $PROJ_PATH/
mvn clean install
##停止项目
killEureka
killConfig
killUpms
killAuth
killGateway


# 删除原有工程
rm -rf $APP_PATH/
mkdir $APP_PATH
cd $APP_PATH/
mkdir we-auth
mkdir we-config
mkdir we-eureka
mkdir we-gateway
mkdir we-upms-service

#复制新的工程
cp $PROJ_PATH/we-eureka/target/we-eureka.jar $APP_PATH/we-eureka/
cp $PROJ_PATH/we-config/target/we-config.jar $APP_PATH/we-config/
cp $PROJ_PATH/we-modules/we-upms-service/target/we-upms-service.jar $APP_PATH/we-upms-service/
cp $PROJ_PATH/we-auth/target/we-auth.jar $APP_PATH/we-auth/
cp $PROJ_PATH/we-gateway/target/we-gateway.jar $APP_PATH/we-gateway/
cp -r /apprun /app/doc/docker

# 启动项目
# 注册服务器
#cd $APP_PATH/we-eureka/
#nohup java -jar we-eureka.jar &
#sleep 20
###配置中心
#cd $APP_PATH/we-config/
#nohup java -jar we-config.jar &
#sleep 30
###统一认证服务
#cd $APP_PATH/we-auth/
#nohup java -jar we-auth.jar &
#sleep 60
### 认证服务器
#$APP_PATH/we-upms-service/
#nohup java -jar we-upms-service.jar &
#sleep 60
###网关
#cd $APP_PATH/we-gateway/
#nohup java -jar  we-gateway.jar &

cd $PROJ_PATH/doc/docker
docker-compose stop
docker-compose build
# 删除无效的images
docker rmi $(docker images | grep "none" | awk '{print $3}')
docker-compose up -d